package com.wjk.kylin.modules.system.position.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wjk.kylin.core.common.aspect.Log;
import com.wjk.kylin.core.common.controller.BaseController;
import com.wjk.kylin.core.common.data.RequestData;
import com.wjk.kylin.core.common.data.ResponseData;
import static com.wjk.kylin.core.constant.CommonConstant.BusinessType.*;

import com.wjk.kylin.core.common.valid.insert;
import com.wjk.kylin.core.common.valid.update;
import com.wjk.kylin.core.utils.PageUtils;
import com.wjk.kylin.modules.system.position.entity.Position;
import com.wjk.kylin.modules.system.position.service.IPositionService;
import com.wjk.kylin.modules.system.vo.ListSelectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 *  职位 前端控制器
 * </p>
 *
 * @author wangjkui
 * @since 2020-06-01
 */
@Api("岗位")
@RestController
@RequestMapping("/sys/position")
public class PositionController extends BaseController {

    @Autowired
    private IPositionService positionService;

    @PreAuthorize("hasAuthority('system:position:list')")
    @ApiOperation("列表")
    @Log(title = "岗位-列表",businessType = LIST,isSaveResponseData = false)
    @GetMapping("/list")
    public ResponseData list(Position position) {
        IPage<Position> page = positionService.findPage(new RequestData<Position>(), position);
        return ResponseData.success().data("page", new PageUtils(page));
    }

    @PreAuthorize("hasAuthority('system:position:query')")
    @ApiOperation("详情")
    @Log(title = "岗位-详情",businessType = GET)
    @GetMapping("/{positionId}")
    public ResponseData get(@PathVariable String positionId) {
        Position position = positionService.getById(positionId);
        return ResponseData.success().data("position", position);
    }

    @PreAuthorize("hasAuthority('system:position:add')")
    @ApiOperation("新增")
    @Log(title = "岗位-新增",businessType = INSERT)
    @PostMapping
    public ResponseData save(@Validated(insert.class) @RequestBody Position position) {
        boolean flag = positionService.save(position);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("新增失败");
        }
    }

    @PreAuthorize("hasAuthority('system:position:edit')")
    @ApiOperation("编辑")
    @Log(title = "岗位-编辑",businessType = UPDATE)
    @PutMapping
    public ResponseData update(@Validated(update.class) @RequestBody Position position) {
        boolean flag = positionService.update(position);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("编辑失败");
        }
    }

    @PreAuthorize("hasAuthority('system:position:delete')")
    @ApiOperation("删除")
    @Log(title = "岗位-删除",businessType = DELETE)
    @DeleteMapping("/{positionIds}")
    public ResponseData delete(@PathVariable String[] positionIds) {

        boolean flag = positionService.deleteByIds(positionIds);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("删除失败");
        }
    }

    @ApiOperation("岗位下拉列表")
    @GetMapping("/select")
    public ResponseData select(){
        List<Position> positionList = positionService.list();

        List<ListSelectVO> list = positionList.stream().map(ListSelectVO::new).collect(Collectors.toList());

        return ResponseData.success().data("list",list);
    }

    @ApiOperation("加载对应用户岗位")
    @GetMapping("/select/{userId}")
    public ResponseData roleUserIdSelect(@PathVariable("userId") String userId){
        List<Position> roleList = positionService.list();

        List<ListSelectVO> list = roleList.stream().map(ListSelectVO::new).collect(Collectors.toList());

        //用户拥有的岗位id
        List<String> checkedKeys = positionService.findPositionListByUserId(userId);


        return ResponseData.success().data("list",list).data("checkedKeys",checkedKeys);
    }
}
